package com.qys.training.base.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.qys.training.base.config.PDFTextConfig;

import java.io.*;

public class addTextUtils{
    public static void addText( PdfStamper pdfStamper, PDFTextConfig config, String fontPath)throws Exception {
        String text = config.getText();
        float x = config.getX();
        float y = config.getY();
        PdfContentByte over = null;
        int pageNum = config.getPageNum();
        over = pdfStamper.getOverContent(pageNum);
        over.beginText();
        BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED);
        over.setFontAndSize(baseFont,8);

        over.showTextAligned(PdfContentByte.ALIGN_CENTER,text,x,y,0);
        over.endText();

        pdfStamper.close();

    }


    public static void overlayFile(String oldPath,String newPath){
        File oldFile = new File(oldPath);
        if (oldFile.exists()){
            try {
                InputStream is = new FileInputStream(oldFile);
                FileOutputStream fos = new FileOutputStream(newPath);
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len=is.read(bytes)) != -1){
                    fos.write(bytes,0,len);
                }
                is.close();
                fos.close();
                oldFile.delete();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

